home *** CD-ROM | disk | FTP | other *** search
/ Aminet 33 / Aminet 33 - October 1999.iso / Aminet / dev / c / GAPLib.lha / GAPLib / wizards / Templates / BoundedRealVector.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-09  |  1.6 KB  |  84 lines

  1.  
  2.  
  3. void Init$N(struct $N *Polly)    $2/* Initialize but keep within constrints. */$
  4. {
  5. int i;
  6. for(i=0;i!=VLENGTH$I;i++) {
  7.     Polly->v[i] = InRand(Constraints$I[i][0],Constraints$I[i][1]);
  8. }
  9. }
  10.  
  11. void Mutate$N(struct $N *Polly)    $2/* Mutate by changeing a value in the vector. */$
  12. {
  13. int i;
  14.  
  15. for(i=0;i!=VLENGTH$I;i++) {
  16.     if(Rnd(1024)==512) {
  17.         Polly->v[i] = InRand(Constraints$I[i][0],Constraints$I[i][1]);
  18.     }
  19. }
  20.  
  21. $4
  22.    NAME
  23.         InRand -- Generate a bounded floating point pseudo-random number.
  24.  
  25.    SYNOPSIS
  26.         double InRand(double,double);
  27.  
  28.         Val = InRand(Lo,Hi);
  29.  
  30.    FUNCTION
  31.         Generates a pseudo random number between Lo and Hi. The resolution of
  32.         the generated number is in steps of (Hi-Lo)/2147483646.
  33. $
  34.  
  35. }
  36.  
  37. $1/* Multipoint crossover for this genome is a numerical crossover. */
  38. $
  39. void Cross$N(struct $N *Polly, struct $N *Tweety)
  40. {
  41. int i;
  42. #ifndef    MPCROSS$I
  43. double t;
  44. i = Rnd(VLENGTH$I+1);
  45. for(;i<VLENGTH$I;i++) {
  46.     t = Polly->v[i];
  47.     Polly->v[i] = Tweety->v[i];
  48.     Tweety->v[i] = t;
  49. }
  50. #else
  51. double    delta,dpos,avg;
  52.  
  53. for(i=0;i!=VLENGTH$I;i++) {
  54.     delta = fabs(Polly->v[i]-Tweety->v[i]);
  55.     if(delta>DBL_EPSILON) {
  56.         dpos = InRand(-delta,delta);
  57.         avg = (Polly->v[i]+Tweety->v[i])/2.0;    $2/* N-dimensional point between the vectors. */$
  58.         Polly->v[i] = avg+dpos;
  59.         Tweety->v[i] = avg-dpos;
  60.     }
  61. }
  62. #endif
  63. }
  64.  
  65. $1/* Standard euclidian length of the difference vector. */
  66. $
  67. double Compare$N(struct $N *Polly,struct $N *Tweety)
  68. {
  69. double    l=0,t;
  70. int i;
  71.  
  72. for(i=0;i!=VLENGTH$I;i++) {
  73.     t = Polly->v[i]-Tweety->v[i];
  74.     l += t*t;
  75. }
  76.  
  77. return(sqrt(l));    $3/* Not quite necessary, but neater. */$
  78. }
  79.  
  80. void Kill$N(struct $N *Polly)
  81. {
  82. ;
  83. }
  84.